/**
 * 文章
 */

const express = require("express");
const { param, body, query } = require("express-validator");
const {
  selectArticleList,
  selectSelfArticleList,
  selectArticle,
  insertArticle,
  updateArticle,
  deleteArticle,
  articleReview,
  articleLike,
} = require("@root/services/blog/article.service.js");
const router = express.Router();
const { checkInterfaceAccessPermission } = require("@root/middleware/permission.js");
const { numberProp, stringProp, article_type, comment_status, status } = {
  numberProp: ([prop, validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage(`${prop}不能为空`)
      .isNumeric({
        no_symbols: true,
      })
      .withMessage(`${prop}必须是正整数`),
  stringProp: ([prop, validator = body] = []) =>
    validator(prop).notEmpty().withMessage(`${prop}不能为空`),
  article_type: ([prop = "article_type", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage(`${prop}不能为空`)
      .isNumeric()
      .withMessage(`${prop}必须是数字类型`),
  comment_status: ([prop = "comment_status", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage(`${prop}不能为空`)
      .isIn([1, 2])
      .withMessage(`${prop}只能是1 or 2`),
  status: ([prop = "status", validator = body] = []) =>
    validator(prop)
      .notEmpty()
      .withMessage("status不能为空")
      .isIn([1, 2, 3])
      .withMessage("status只能是1 or 2 or 3"),
};

/**
 * 获取全部文章列表
 */
router.get(
  "/list",
  [
    numberProp(["currentPage", query]),
    numberProp(["pageSize", query]),
    status(["status", query]),
    // query("cate_id").isInt({ min: 1 }).withMessage("cate_id必须是正整数"),
    query().custom((value) => {
      const { tag_ids } = value;
      if (!tag_ids && tag_ids !== "") {
        const reg = new RegExp(/^\d+(,\d+)*$/);
        if (!reg.test(tag_ids)) throw new Error("tag_ids参数为:'1,3,4'");
      }
      return true;
    }),
  ],
  selectArticleList
);
/**
 * 获取个人全部文章列表
 */
router.get(
  "/list/self",
  [
    numberProp(["currentPage", query]),
    numberProp(["pageSize", query]),
    status(["status", query]),
    // query("cate_id").isInt({ min: 1 }).withMessage("cate_id必须是正整数"),
    query().custom((value) => {
      const { tag_ids } = value;
      if (!tag_ids && tag_ids !== "") {
        const reg = new RegExp(/^\d+(,\d+)*$/);
        if (!reg.test(tag_ids)) throw new Error("tag_ids参数为:'1,3,4'");
      }
      return true;
    }),
  ],
  selectSelfArticleList
);

/**
 * 获取某个文章
 */
router.get("/:article_id", [numberProp(["article_id", param])], selectArticle);

const add_edit_params = [
  stringProp(["article_title", body]),
  stringProp(["article_digest", body]),
  stringProp(["article_content", body]),
  article_type(),
  comment_status(),
  body("cate_id").isInt({ min: 1 }).withMessage("cate_id必须是正整数"),
  body("tag_ids").isArray({ min: 1 }).withMessage("tag_ids必须是数组,且不能为空"),
  body("tag_ids.*").isInt({ min: 1 }).withMessage("tag_ids中的元素必须是正整数"),
];
/**
 * 新增文章
 */
router.post("/", add_edit_params, insertArticle);

/**
 * 修改文章
 */
router.put("/", [...add_edit_params, numberProp(["article_id", body]), status()], updateArticle);

/**
 * 删除文章
 */
router.delete(
  "/",
  [
    body("ids").isArray({ min: 1 }).withMessage("ids必须是数组,且不能为空"),
    body("ids.*").isInt({ min: 1 }).withMessage("数组中的元素必须是正整数"),
  ],
  checkInterfaceAccessPermission(['article:list:delete']), // 注入接口权限判定
  deleteArticle
);

/**
 * 文章审核 Article review
 */
router.patch(
  "/review",
  [
    numberProp(["article_id", body]),
    body("status").isIn([1, 2, 3]).withMessage("status只能是1 or 2 or 3"),
    // body("noPass_reason").isString().withMessage("未通过原因必须是字符串"),
  ],
  checkInterfaceAccessPermission(['article:review']), // 注入接口权限判定
  articleReview
);

/**
 * 文章 点赞和取消
 */
router.patch("/like/:article_id", [numberProp(["article_id", param])], articleLike);

module.exports = router;
